package com.shanghaionstar.user.presenter;

import android.os.Bundle;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.TermsConditionRes;
import com.shanghaionstar.user.iview.ITermsConditionsView;
import com.shanghaionstar.user.service.TermsConditionsServices;

import java.util.List;

import rx.functions.Action1;

/**
 * Created by 7828wc on 2016/6/29.
 */
public class TermsConditionsPresenter extends BasePresenter<List<TermsConditionRes>> {
    private BaseActivity mContext;
    private ITermsConditionsView mDetailView;
    private TermsConditionsServices mService;

    public TermsConditionsPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<List<TermsConditionRes>> view) {
        super.setView(view);
        mContext = (BaseActivity) view.onActivity();
        mService = new TermsConditionsServices(mContext);
        mDetailView = (ITermsConditionsView) view;
    }

    public void initData(Bundle bundle) {
        getCategoryContentById();
    }

    public void getCategoryContentById(){
        mDetailView.setLoading(true);
        subscription=mService.getCategoryContentById().subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<List<TermsConditionRes>>() {
                    @Override
                    public void call(List<TermsConditionRes> contentList) {
                        mDetailView.setLoading(false);
                        mDetailView.onReponseSuccess(contentList);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mDetailView.setLoading(false);
                        if(throwable != null)
                            mDetailView.onResponseError((OnStarException) throwable);
                    }
                });
    }


}
